/**
 * 保存完善后的个人资料
 */
var mongodb=require('./db');
function Wanshan(name,password,email,tel,gangwei,xueli,yuexin,ruzhiDate) {
    this.name=name;
    this.password=password;
    this.email=email;
    this.tel=tel;
    this.gangwei=gangwei;
    this.xueli=xueli;
    this.yuexin=yuexin;
    this.ruzhiDate=ruzhiDate;
}
module.exports=Wanshan;
Wanshan.prototype.save=function (callback) {
    var date=new Date();
    var data={
        name:this.name,
        password:this.password,
        email:this.email,
        tel:this.tel,
        gangwei:this.gangwei,
        xueli:this.xueli,
        yuexin:this.yuexin,
        ruzhiDate:this.ruzhiDate,
        //保存资料的时间
        second:date.getFullYear()+'-'+((date.getMonth()+1)<10?'0'+(date.getMonth()+1):(date.getMonth()+1))+'-'+
        (date.getDate()<10?'0'+date.getDate():date.getDate())+' '+(date.getHours()<10?'0'+date.getHours():date.getHours())+
        ':'+(date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes())+':'+(date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds())

    }
    //打开数据库
    mongodb.open(function (err,db) {
        if(err){
            return callback(err);
        }
        //读取数据库的集合,不存在会自动创建
        db.collection('wanshans',function (err,collection) {
            if(err){
                mongodb.close();
                return callback(err);
            }
            collection.insert(data,{safe:true},function (err) {
                mongodb.close();
                if(err){
                    return callback(err);
                }
                //保存成功
                return callback(null);
            })
        });
    })
}
//查询个人资料
Wanshan.get=function (name,callback) {
    var query={
        name:name
    }
    mongodb.open(function (err,db) {
        if(err){
            return callback(err);
        }
        db.collection('wanshans',function (err,collection) {
            if(err){
                mongodb.close();
                return callback(err);
            }
            collection.find(query).sort({second:-1}).toArray(function (err,wanshans) {
                mongodb.close();
                if(err){
                    return callback(err);
                }
                //查询成功
                return callback(null,wanshans);
            })
        })
    })
}
